package com.pllandxzx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pll.dto.ResponseDto;
import com.pll.pojo.Card;
import com.pllandxzx.dto.ResponseDTO;
import com.pllandxzx.mapper.MarketingMapper;
import com.pllandxzx.mapper.UserMapper;
import com.pllandxzx.service.MarketingService;
import com.pllandxzx.vo.AddManageCardVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



/**
 * @ClassName MarketingServiceImpl
 * @Description TODO
 * @Author 小鹿
 * @Date 2025/6/13 9:55
 */

@Service
public class MarketingServiceImpl implements MarketingService
{
    @Autowired
    private MarketingMapper marketingMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public ResponseDto getAllMarketingList(String activityName, Integer page, Integer pageSize, Integer type)
    {
        LambdaQueryWrapper<Card> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<Card> cardPage = new Page<>(page,pageSize);
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(activityName), Card::getCardName, activityName)
                .eq(Card::getMerId,type);
        Page<Card> resultPage = marketingMapper.selectPage(cardPage, lambdaQueryWrapper);
        System.out.println(resultPage);
        if (resultPage != null && !resultPage.getRecords().isEmpty()) {
            return ResponseDto.success(resultPage);
        }
        return ResponseDto.fail(405, "未查询到数据");
    }

    @Override
    public ResponseDto addManageCard(AddManageCardVo addManageCardVo)
    {
        // 创建实体对象
        Card card = new Card();

        card.setCardName(addManageCardVo.getActivityName());
        card.setCardNum(addManageCardVo.getCouponCount());
        card.setExpireTime(addManageCardVo.getExpireTime());
        card.setCreateTime(addManageCardVo.getCreateTime());
        card.setCardPrice(addManageCardVo.getCardPrice());
        card.setCondition(addManageCardVo.getCondition());
        card.setEndTime(addManageCardVo.getEndTime());
        return ResponseDto.success(marketingMapper.insert(card));
    }

    @Override
    public ResponseDto delete(Integer cardId)
    {
        //如果用户有这张优惠券，则不能删除
        if (userMapper.userCardCount(cardId) != 0){
            return ResponseDto.fail(405,"用户已领取该优惠券，不能删除，可以尝试下架优惠券");
        }

        LambdaQueryWrapper<Card> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Card::getCardId,cardId);
        if (marketingMapper.delete(lambdaQueryWrapper)>0){
            return ResponseDto.success("删除成功");
        }
        return ResponseDto.fail(405,"删除失败");
    }

    @Override
    public ResponseDto edit(Integer cardId, AddManageCardVo addManageCardVo)
    {
        // 创建实体对象
        Card card = new Card();
        card.setCardId(cardId);
        card.setMerId(addManageCardVo.getMerId());
        card.setCardName(addManageCardVo.getActivityName());
        card.setCardPrice(addManageCardVo.getCardPrice());
        card.setCondition(addManageCardVo.getCondition());
        card.setCardNum(addManageCardVo.getCouponCount());
        card.setCreateTime(addManageCardVo.getCreateTime());
        card.setEndTime(addManageCardVo.getEndTime());
        card.setExpireTime(addManageCardVo.getExpireTime());
        if (marketingMapper.updateById(card)>0){
            return ResponseDto.success("修改成功");
        }
        return ResponseDto.fail(405,"修改失败");
    }

    @Override
    public ResponseDTO offlineCard(AddManageCardVo addManageCardVo) {
        Integer i = marketingMapper.offlineCard(addManageCardVo);
        if (!i.equals(0)){
            return ResponseDTO.Success(200,"下架成功");

        }
        return ResponseDTO.Fail(405,"下架失败");
    }
}